package com.codermn.day802;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author jxau_scf
 * @date 2021/8/2 - 9:17
 */
public class TestCondition {
    static ReentrantLock lock = new ReentrantLock();
    static boolean isWoman = false;
    static Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        //jack
        new Thread(()->{
            lock.lock();
            while (!isWoman){
                System.out.println("没有女朋友，我等老板安排，先休息，待会工作");
                try {
                    //jack线程进入阻塞，但释放了锁
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("开始工作");
            lock.unlock();
        }, "jack").start();

        TimeUnit.SECONDS.sleep(1);

        System.out.println("jack释放了锁，主线程可以得到锁");

        lock.lock();
        for (int i = 0; i < 5; i++){
            new Thread(()->{
                lock.lock();
                System.out.println("默默无闻的程序员codeing");
                lock.unlock();
            }, "t"+i).start();
            TimeUnit.MILLISECONDS.sleep(1);
        }

        //注意顺序换了
        isWoman = true;
        condition.signalAll();
        lock.unlock();


    }

}
